<?php

namespace app\index\model;
use think\Db;
use think\Model;
use think\Session;

class OrderModel extends Model
{
    public function search_mess()
    {
        // var_dump(session::get('userid'));die;
       if(empty(session::get('userid'))){
           $userid = $_COOKIE['userid'];
       }else{
           $userid = session::get('userid');
       }
        // var_dump($userid);die;
        $mess = Db::table('user')->field('nickname, tel') ->where('id',$userid) ->select();
       // var_dump($mess);die;

        $cartmess = Db::table('cart') ->field('*') ->where('userid',$userid) ->select();
       // var_dump($cartmess);die;

        $cid = Db::table('cart')->field('goodsid')->where('userid',$userid)->select();
       // var_dump($cid);die;
        for($a=0; $a<count($cid); $a++){
            $ppic = Db::table('goods') ->field('pictrue, name') ->where('id',$cid[$a]['goodsid']) ->select();
            $pic[$a] = $ppic[0];
        }

       // var_dump($pic);die;
        for($b=0; $b<count($pic); $b++){
            $cartmess[$b]['pictrue'] = $pic[$b]['pictrue'];
            $cartmess[$b]['goodsname'] = $pic[$b]['name'];
        }

        $cartmess[0]['name'] = $mess[0]['nickname'];
        $cartmess[0]['tel'] = $mess[0]['tel'];
//        var_dump($cartmess);die;

        return $cartmess;
    }
    public function insert_mess()
    {
        if(empty(session::get('userid'))){
           $userid = $_COOKIE['userid'];
       }else{
           $userid = session::get('userid');
       }
        // $userid = 6;
       // var_dump($_POST);die;
        $Omess = $_POST;

        $Imess = Db::table('cart') ->field('*') ->where('userid',$userid) ->select();
       // var_dump($Imess);die;

        for($k=0; $k<count($Imess); $k++){
            $Imess[$k]['goods_Mprice'] = $Imess[$k]['count']*$Imess[$k]['price'];
            $O_Gmess[$k]['price'] = $Imess[$k]['goods_Mprice'];
            $O_Gmess[$k]['count'] = $Imess[$k]['count'];
            $O_Gmess[$k]['gid'] = $Imess[$k]['goodsid'];

        }
        
        $Imess[0]['Allcount'] = 0;
        $Imess[0]['Allprice'] = 0;
        for($i=0; $i<(count($Imess)); $i++){
            $Imess[0]['Allcount'] += $Imess[$i]['count'];
            $Imess[0]['Allprice'] += $Imess[$i]['goods_Mprice'];
        }
        $Omess['price'] = $Imess[0]['Allprice'];
        $Omess['count'] = $Imess[0]['Allcount'];
        $Omess['userid'] = $userid;
        unset($Omess['name']);
        unset($Omess['tel']);
       // var_dump($Omess);die;

        $Insert_O = Db::table('order') ->insert($Omess);

//        var_dump($this->getLastInsID());die;

        for($y=0; $y<count($Imess); $y++){
            $O_Gmess[$y]['oid'] = $this->getLastInsID();
        }
       // var_dump($O_Gmess);die;

        for($z=0; $z<count($O_Gmess); $z++){
            $Insert_OG = Db::table('order_goods') ->insert($O_Gmess[$z]);
        }
//        var_dump($Insert_OG);die;

        $del_cart = Db::table('cart') ->where('userid',$userid) ->delete();

        if($Insert_O > 0 && $del_cart){
            return $Insert_OG;
        }else{
            return false;
        }
    }

}













